package ar.edu.sovietrussia;

import java.util.HashMap;
import java.util.Map;

import org.apache.wicket.Component;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import services.book.BookService;
import services.user.PersonService;
import ar.edu.sovietrussia.bars.MenuBar;
import ar.edu.sovietrussia.customcomponents.login.LoginPanel;
import ar.edu.sovietrussia.customcomponents.searchbook.MainSearchComponent;
import ar.edu.sovietrussia.customcomponents.util.WrapperPanel;

public class LibraryApplicationPage extends WebPage {
    private static final long serialVersionUID = 1L;

    // --------------------------------------------------------//

    @SpringBean(name = "service.person")
    private PersonService personService;

    @SpringBean
    private BookService bookService;

    private WrapperPanel topPanel;

    private WrapperPanel centerPanel;

    private WrapperPanel rightPanel;

    private Component menuBar;

    private FeedbackPanel feedbackPanel;

    // --------------------------------------------------------//

    public LibraryApplicationPage() {
        super();
        this.addComponents();

    }

    private void addComponents() {

        this.add(new Label("message_bibliotecaParaTodos", "Biblioteca Para Todos"));

        this.topPanel = new WrapperPanel(this.getTopPanelId(), this.generateSignInPanel(WrapperPanel.INTERNALID));
        this.add(this.topPanel);

        this.centerPanel = new WrapperPanel(this.getCenterPanelId(),
                this.createMainSearchComponent(WrapperPanel.INTERNALID));
        this.add(this.centerPanel);

        this.menuBar = this.createMenuBar("menuBar");
        this.add(this.menuBar);

        this.feedbackPanel = new FeedbackPanel("fbPanel");
        this.feedbackPanel.setOutputMarkupId(true);
        this.add(this.feedbackPanel);
    }

    private Component createMenuBar(final String id) {

        MenuBar bar = new MenuBar(id) {

            private static final long serialVersionUID = 1L;

            @Override
            public Map<String, WrapperPanel> getReplaceableComponents() {
                return LibraryApplicationPage.this.replaceableComponentsMap();
            }

            @Override
            public FeedbackPanel getFeedbackTarget() {
                return LibraryApplicationPage.this.getFeedbackPanel();
            }

        };
        return bar;
    }

    // --------------------------------------------------------//

    public Panel createMainSearchComponent(final String id) {
        return new MainSearchComponent(id) {
            private static final long serialVersionUID = 1L;

            @Override
            public FeedbackPanel getFeedbackTarget() {
                return LibraryApplicationPage.this.getFeedbackPanel();
            }

            @Override
            public Map<String, WrapperPanel> getReplaceableComponents() {
                return LibraryApplicationPage.this.replaceableComponentsMap();
            }

        };
    }

    public Panel generateSignInPanel(final String id) {
        Panel result = new LoginPanel(id) {

            private static final long serialVersionUID = 1L;

            @Override
            public FeedbackPanel getFeedbackTarget() {
                return LibraryApplicationPage.this.getFeedbackPanel();
            }

            @Override
            public Map<String, WrapperPanel> getReplaceableComponents() {
                return LibraryApplicationPage.this.replaceableComponentsMap();
            }

        };

        return result;
    }

    public void changeCenterPanel(final Component component) {
        this.getCenterPanel().replaceContent(component);
    }

    public void changeTopPanel(final Component component) {
        this.getTopPanel().replaceContent(component);
    }

    public String getTopPanelId() {
        return "topPanel";
    }

    // ----------ACCESSORS------------------------------------//

    public String getCenterPanelId() {
        return "centerPanel";
    }

    public void setPersonService(final PersonService personService) {
        this.personService = personService;
    }

    public PersonService getPersonService() {
        return this.personService;
    }

    public WrapperPanel getTopPanel() {
        return this.topPanel;
    }

    public BookService getBookService() {
        return this.bookService;
    }

    public void setBookService(final BookService bookService) {
        this.bookService = bookService;
    }

    public void setMenuBar(final Component menuBar) {
        this.menuBar = menuBar;
    }

    public Component getMenuBar() {
        return this.menuBar;
    }

    public WrapperPanel getCenterPanel() {
        return this.centerPanel;
    }

    public void setCenterPanel(final WrapperPanel centerPanel) {
        this.centerPanel = centerPanel;
    }

    public WrapperPanel getRightPanel() {
        return this.rightPanel;
    }

    public void setRightPanel(final WrapperPanel rightPanel) {
        this.rightPanel = rightPanel;
    }

    public void setTopPanel(final WrapperPanel topPanel) {
        this.topPanel = topPanel;
    }

    protected String getUsableId() {
        return WrapperPanel.INTERNALID;
    }

    // --------------------------------------------------------//

    public void setFeedbackPanel(final FeedbackPanel feedbackPanel) {
        this.feedbackPanel = feedbackPanel;
    }

    public FeedbackPanel getFeedbackPanel() {
        return this.feedbackPanel;
    }

    public Map<String, WrapperPanel> replaceableComponentsMap() {
        Map<String, WrapperPanel> map = new HashMap<String, WrapperPanel>();
        map.put("topPanel", this.getTopPanel());
        map.put("centerPanel", this.getCenterPanel());

        return map;
    }

}
